﻿using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data;
using Hidistro.Core;

namespace wnxd.product
{
    public class wnxd_product
    {
        private Database database;
        public wnxd_product()
        {
            this.database = DatabaseFactory.CreateDatabase();
        }
        public DataRow TagGetProduct(int categoryid, int tag)
        {
            using (DbCommand cmd = database.GetSqlStringCommand(string.Format("select top 1 * from Hishop_Products where MainCategoryPath like '{0}|%' and ProductId in (select ProductId from Hishop_ProductTag where TagId = {1})", categoryid, tag)))
            using (IDataReader reader = database.ExecuteReader(cmd))
            {
                DataTable table = DataHelper.ConverDataReaderToDataTable(reader);
                if (table == null || table.Rows.Count == 0) return null;
                return table.Rows[0];
            }
        }
        public DataRow TagGetProduct(int categoryid, int tag, int index)
        {
            if (index <= 0) return null;
            using (DbCommand cmd = database.GetSqlStringCommand(string.Format("select top {2} * from Hishop_Products where MainCategoryPath like '{0}|%' and ProductId in (select ProductId from Hishop_ProductTag where TagId = {1})", categoryid, tag, index)))
            using (IDataReader reader = database.ExecuteReader(cmd))
            {
                DataTable table = DataHelper.ConverDataReaderToDataTable(reader);
                if (table == null || table.Rows.Count < index) return null;
                return table.Rows[index - 1];
            }
        }
    }
}
